﻿using Nop.Core.Configuration;
using Nop.Core.Domain.Blogs;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Forums;
using Nop.Core.Domain.Messages;
using Nop.Core.Domain.News;
using Nop.Core.Domain.Orders;
using Nop.Core.Domain.Shipping;
using Nop.Core.Domain.Vendors;
using Nop.Services.Common;
using Nop.Services.Messages;
using Nop.Web.Services.Easemob;
using Nop.Web.Services.Mob;
using Nop.Web.Services.Utils;
using System;
using System.Collections.Generic;
using System.Text;

namespace Nop.Web.Services.Messages
{
    public class WorkflowMessageService : IWorkflowMessageService
    {
        readonly MobPushHelper _pushHelper;
        private readonly IGenericAttributeService _genericAttributeService;
        private readonly HUCSdkHelper _hUCSdkHelper;

        public WorkflowMessageService(MobPushHelper pushHelper, IGenericAttributeService genericAttributeService, HUCSdkHelper hUCSdkHelper)
        {
            this._pushHelper = pushHelper;
            _genericAttributeService = genericAttributeService;
            _hUCSdkHelper = hUCSdkHelper;
        }

        #region 待实现

        /// <summary>
        /// 库存告警
        /// </summary>
        /// <param name="subscription"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public IList<int> SendBackInStockNotification(BackInStockSubscription subscription, int languageId)
        {
            return new List<int>();
        }

        /// <summary>
        /// /博客新评论
        /// </summary>
        /// <param name="blogComment"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public IList<int> SendBlogCommentNotificationMessage(BlogComment blogComment, int languageId)
        {
            return new List<int>();
        }

        /// <summary>
        /// 联系我们(发送邮件给管理员)
        /// </summary>
        /// <param name="languageId"></param>
        /// <param name="senderEmail"></param>
        /// <param name="senderName"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public IList<int> SendContactUsMessage(int languageId, string senderEmail, string senderName, string subject, string body)
        {
            return new List<int>();
        }

        /// <summary>
        /// 联系供应商(发送邮件给供应商)
        /// </summary>
        /// <param name="vendor"></param>
        /// <param name="languageId"></param>
        /// <param name="senderEmail"></param>
        /// <param name="senderName"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public IList<int> SendContactVendorMessage(Vendor vendor, int languageId, string senderEmail, string senderName, string subject, string body)
        {
            return new List<int>();
        }

        /// <summary>
        /// 注册时发送邮箱验证邮件
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public IList<int> SendCustomerEmailRevalidationMessage(Customer customer, int languageId)
        {
            return new List<int>();
        }

        /// <summary>
        /// 邮箱验证成功后发送邮件
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public IList<int> SendCustomerEmailValidationMessage(Customer customer, int languageId)
        {
            return new List<int>();
        }

        /// <summary>
        /// 密码重置消息
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public IList<int> SendCustomerPasswordRecoveryMessage(Customer customer, int languageId)
        {
            return new List<int>();
        }

        /// <summary>
        /// 注册后发送邮件通知
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public IList<int> SendCustomerRegisteredNotificationMessage(Customer customer, int languageId)
        {
            return new List<int>();
        }

        /// <summary>
        /// 注册成功后发送欢迎邮件
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public IList<int> SendCustomerWelcomeMessage(Customer customer, int languageId)
        {
            return new List<int>();
        }

        /// <summary>
        /// 优惠券通知
        /// </summary>
        /// <param name="giftCard"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public IList<int> SendGiftCardNotification(GiftCard giftCard, int languageId)
        {
            return new List<int>();
        }

        /// <summary>
        /// 论坛回复通知
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="forumPost"></param>
        /// <param name="forumTopic"></param>
        /// <param name="forum"></param>
        /// <param name="friendlyForumTopicPageIndex"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public IList<int> SendNewForumPostMessage(Customer customer, ForumPost forumPost, ForumTopic forumTopic, Forum forum, int friendlyForumTopicPageIndex, int languageId)
        {
            return new List<int>();
        }

        /// <summary>
        /// 论坛新帖子通知相关订阅者
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="forumTopic"></param>
        /// <param name="forum"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public IList<int> SendNewForumTopicMessage(Customer customer, ForumTopic forumTopic, Forum forum, int languageId)
        {
            return new List<int>();
        }



        /// <summary>
        /// 当有退货提交时通知客户
        /// </summary>
        /// <param name="returnRequest"></param>
        /// <param name="orderItem"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public IList<int> SendNewReturnRequestCustomerNotification(ReturnRequest returnRequest, OrderItem orderItem, int languageId)
        {
            return new List<int>();
        }

        /// <summary>
        /// 有退货提交时通知店铺管理员
        /// </summary>
        /// <param name="returnRequest"></param>
        /// <param name="orderItem"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public IList<int> SendNewReturnRequestStoreOwnerNotification(ReturnRequest returnRequest, OrderItem orderItem, int languageId)
        {
            return new List<int>();
        }

        /// <summary>
        /// Sends a news comment notification message to a store owner
        /// </summary>
        /// <param name="newsComment"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public IList<int> SendNewsCommentNotificationMessage(NewsComment newsComment, int languageId)
        {
            return new List<int>();
        }

        /// <summary>
        /// Sends a newsletter subscription activation message
        /// </summary>
        /// <param name="subscription"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public IList<int> SendNewsLetterSubscriptionActivationMessage(NewsLetterSubscription subscription, int languageId)
        {
            return new List<int>();
        }

        /// <summary>
        ///  Sends a newsletter subscription deactivation message
        /// </summary>
        /// <param name="subscription"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public IList<int> SendNewsLetterSubscriptionDeactivationMessage(NewsLetterSubscription subscription, int languageId)
        {
            return new List<int>();
        }

        /// <summary>
        ///  Sends a "new VAT submitted" notification to a store owner
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="vatName"></param>
        /// <param name="vatAddress"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public IList<int> SendNewVatSubmittedStoreOwnerNotification(Customer customer, string vatName, string vatAddress, int languageId)
        {
            return new List<int>();
        }

        /// <summary>
        ///  Sends 'New vendor account submitted' message to a store owner
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="vendor"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public IList<int> SendNewVendorAccountApplyStoreOwnerNotification(Customer customer, Vendor vendor, int languageId)
        {
            return new List<int>();
        }

        public int SendNotification(MessageTemplate messageTemplate, EmailAccount emailAccount, int languageId, IEnumerable<Token> tokens, string toEmailAddress, string toName, string attachmentFilePath = null, string attachmentFileName = null, string replyToEmailAddress = null, string replyToName = null, string fromEmail = null, string fromName = null, string subject = null)
        {
            return 0;
        }




        public IList<int> SendOrderCompletedCustomerNotification(Order order, int languageId, string attachmentFilePath = null, string attachmentFileName = null)
        {
            return new List<int>();
        }

        public IList<int> SendOrderPaidAffiliateNotification(Order order, int languageId)
        {
            return new List<int>();
        }

     

        public IList<int> SendOrderPaidStoreOwnerNotification(Order order, int languageId)
        {
            return new List<int>();
        }

        public IList<int> SendOrderPaidVendorNotification(Order order, Vendor vendor, int languageId)
        {
            return new List<int>();
        }

        public IList<int> SendOrderPlacedAffiliateNotification(Order order, int languageId)
        {
            return new List<int>();
        }

        public IList<int> SendOrderPlacedCustomerNotification(Order order, int languageId, string attachmentFilePath = null, string attachmentFileName = null)
        {
            return new List<int>();
        }

        public IList<int> SendOrderPlacedStoreOwnerNotification(Order order, int languageId)
        {
            return new List<int>();
        }

        public IList<int> SendOrderPlacedVendorNotification(Order order, Vendor vendor, int languageId)
        {
            return new List<int>();
        }

        public IList<int> SendOrderRefundedCustomerNotification(Order order, decimal refundedAmount, int languageId)
        {
            return new List<int>();
        }

        public IList<int> SendOrderRefundedStoreOwnerNotification(Order order, decimal refundedAmount, int languageId)
        {
            return new List<int>();
        }

        public IList<int> SendPrivateMessageNotification(PrivateMessage privateMessage, int languageId)
        {
            return new List<int>();
        }

        public IList<int> SendProductEmailAFriendMessage(Customer customer, int languageId, Product product, string customerEmail, string friendsEmail, string personalMessage)
        {
            return new List<int>();
        }

        public IList<int> SendProductReviewNotificationMessage(ProductReview productReview, int languageId)
        {
            return new List<int>();
        }

        public IList<int> SendProductReviewReplyCustomerNotificationMessage(ProductReview productReview, int languageId)
        {
            return new List<int>();
        }

        public IList<int> SendQuantityBelowStoreOwnerNotification(Product product, int languageId)
        {
            return new List<int>();
        }

        public IList<int> SendQuantityBelowStoreOwnerNotification(ProductAttributeCombination combination, int languageId)
        {
            return new List<int>();
        }

        public IList<int> SendRecurringPaymentCancelledCustomerNotification(RecurringPayment recurringPayment, int languageId)
        {
            return new List<int>();
        }

        public IList<int> SendRecurringPaymentCancelledStoreOwnerNotification(RecurringPayment recurringPayment, int languageId)
        {
            return new List<int>();
        }

        public IList<int> SendRecurringPaymentFailedCustomerNotification(RecurringPayment recurringPayment, int languageId)
        {
            return new List<int>();
        }

        public IList<int> SendReturnRequestStatusChangedCustomerNotification(ReturnRequest returnRequest, OrderItem orderItem, int languageId)
        {
            return new List<int>();
        }

    

        public int SendTestEmail(int messageTemplateId, string sendToEmail, List<Token> tokens, int languageId)
        {
            return 0;
        }

        public IList<int> SendVendorInformationChangeNotification(Vendor vendor, int languageId)
        {
            return new List<int>();
        }

        public IList<int> SendWishlistEmailAFriendMessage(Customer customer, int languageId, string customerEmail, string friendsEmail, string personalMessage)
        {
            return new List<int>();
        }

        #endregion


        /// <summary>
        /// 订单备注通知,当订单备注为对客户显示时发送
        /// </summary>
        /// <param name="orderNote"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public IList<int> SendNewOrderNoteAddedCustomerNotification(OrderNote orderNote, int languageId)
        {
            var ucMemberId = _hUCSdkHelper.GetMemberId(orderNote.Order.Customer); 
            if (ucMemberId > 0)
            {
                string mobAlia = string.Format("huc{0}", ucMemberId);
                _pushHelper.Send(mobAlia, "订单状态变更", $"您的订单[{ orderNote.Order.CustomOrderNumber}]:{orderNote.Note}", new
                {
                    memberId = ucMemberId,
                    type = "orderNote",
                    ext = new
                    {
                        orderId = orderNote.Order.Id
                    }
                });
            }
            return new List<int>();
        }

        /// <summary>
        /// 发货时通知
        /// </summary>
        /// <param name="shipment"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public IList<int> SendShipmentSentCustomerNotification(Shipment shipment, int languageId)
        {
            var ucMemberId = _hUCSdkHelper.GetMemberId(shipment.Order.Customer); 
            if (ucMemberId > 0)
            {
                string mobAlia = string.Format("huc{0}", ucMemberId);
                _pushHelper.Send(mobAlia, "订单状态变更", $"您的订单[{ shipment.Order.CustomOrderNumber}]已发货,包裹编号:{shipment.Id}", new
                {
                    memberId = ucMemberId,
                    type = "orderNote",
                    ext = new
                    {
                        orderId = shipment.Order.Id
                    }
                });
            }
            return new List<int>();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="order"></param>
        /// <param name="languageId"></param>
        /// <returns></returns>
        public IList<int> SendOrderCancelledCustomerNotification(Order order, int languageId)
        {
            var ucMemberId = _hUCSdkHelper.GetMemberId(order.Customer);
            if (ucMemberId > 0)
            {
                string mobAlia = string.Format("huc{0}", ucMemberId);
                _pushHelper.Send(mobAlia, "订单状态变更", $"您的订单[{order.CustomOrderNumber}]已被系统取消", new
                {
                    memberId = ucMemberId,
                    type = "orderNote",
                    ext = new
                    {
                        orderId = order.Id
                    }
                });
            }
            return new List<int>();
        }


        public IList<int> SendShipmentDeliveredCustomerNotification(Shipment shipment, int languageId)
        {
            var ucMemberId = _hUCSdkHelper.GetMemberId(shipment.Order.Customer);
            if (ucMemberId > 0)
            {
                string mobAlia = string.Format("huc{0}", ucMemberId);
                _pushHelper.Send(mobAlia, "订单状态变更", $"您的订单[{shipment.Order.CustomOrderNumber}]包裹编号[{shipment.Id}]已被系统自动收货", new
                {
                    memberId = ucMemberId,
                    type = "orderNote",
                    ext = new
                    {
                        orderId = shipment.Order.Id
                    }
                });
            }
            return new List<int>();
        }

        public IList<int> SendOrderPaidCustomerNotification(Order order, int languageId, string attachmentFilePath = null, string attachmentFileName = null)
        {
            var ucMemberId = _hUCSdkHelper.GetMemberId(order.Customer);
            if (ucMemberId > 0)
            {
                string mobAlia = string.Format("huc{0}", ucMemberId);
                _pushHelper.Send(mobAlia, "订单状态变更", $"您的订单[{order.CustomOrderNumber}]已支付成功", new
                {
                    memberId = ucMemberId,
                    type = "orderNote",
                    ext = new
                    {
                        orderId = order.Id
                    }
                });
            }
            return new List<int>();
        }
    }
}
